package com.ws.dll.impl;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/*
 * MIS-POS程序调用
 */
public class MisDll {

	public static HashMap<String, String> dataMap = ParseMapInfo.intDataMap();
	// 初始化
	public static int init(String comtype, String param1, String param2) {
		try {
			int ret = -4;
			if (comtype.equals("1"))
				ret = MisDllJna.instanceDll.com_typeset(1);
			else if (comtype.equals("2"))
				ret = MisDllJna.instanceDll.com_typeset(2);

			System.out.println("com_typeset:" + ret);

			if (ret == 0) {
				if (comtype.equals("1"))
					ret = MisDllJna.instanceDll.com_setup(Integer
							.parseInt(param1), Integer.parseInt(param1));
				else if (comtype.equals("2"))
					ret = MisDllJna.instanceDll.com_IPset(Integer
							.parseInt(param2), param1);
			}
			return ret;
		} catch (Exception e) {
			System.out.println(e.toString());
			return 9;
		}

	}



	/**
	 * 解析返回报文格式
	 * 格式（去掉中框号）[四位len][2位retcode][剩余data域]
	 * @param rspdata
	 * @return
	 */
	public static Map<String, String> parseRetMsg(String rspdata) {
		byte[] respByte = rspdata.getBytes(Charset.forName("GBK"));

		byte[] len_b = new byte[4];
		byte[] ret = new byte[2];
		System.arraycopy(respByte, 0, len_b, 0, 4);
		int len = Integer.parseInt(new String(len_b));
		System.arraycopy(respByte, 4, ret, 0, 2);
		String retCode = new String(ret);
		Map<String, String> retMap = new HashMap<>(20);
		retMap.put("交易应答码", retCode);
		int index = 6;
		//解析返回Data域名， 都是LLvar DATA格式详表：（类型：L代表1位数字，LLvar：表示2位长度，数据是字符型，数据长度是LL）
		while(index < len) {
			byte[] tag = new byte[2];
			System.arraycopy(respByte, index, tag, 0, 2);
			String llTag = new String(tag);
			index = index + 2;
			byte[] len_d = new byte[2];
			System.arraycopy(respByte, index, len_d, 0, 2);
			int lend = Integer.parseInt(new String(len_d));
			index = index + 2;
			byte[] ll_data = new byte[lend];
			System.arraycopy(respByte, index, ll_data, 0, lend);
			String llData = new String(ll_data, Charset.forName("GBK"));
			// 已实际长度为准
			index = index + lend;
			retMap.put(dataMap.get(llTag), llData);
		}
		return retMap;
	}

	public static void main(String[] args) {
		// ------------------具体接口请参照文档---------------------------//
		// 此处防止dll返回的信息乱码
		System.setProperty("jna.encoding", "GBK");
		// 区别终端名称
		String APP = "MISAPP1";
		int ret = 9;
		// 设置大一点，避免IndexOutOfBoundsException
		StringByReference retdata = new StringByReference(2048);
		if (args.length >= 3)
			ret = MisDll.init(args[0], args[1], args[2]);
		else {
			// 签到
//			 ret = MisDllJna.instanceDll.checkin_trans(APP, retdata);
			// 查询余额
//			ret = MisDllJna.instanceDll.balance_trans(APP, retdata);
			// 消费
//			ret = MisDllJna.instanceDll.sale_trans(APP, "1000", retdata);

			// 消费撤销， 000000- pos端输入traceno,  -- 预授权发卡放不同意
//			ret = MisDllJna.instanceDll.void_trans(APP, "000128", "1000", retdata);

			// 预授权
//			ret = MisDllJna.instanceDll.pre_trans(APP, "1000", retdata);

			// 预授权撤销
//			ret = MisDllJna.instanceDll.preVoid_trans(APP, "1000","122", "1114", "", retdata);

			// 预授权完成
//			ret = MisDllJna.instanceDll.preCom_trans(APP, "1000","122", "1114", retdata);

			// 预授权完成撤销
//			ret = MisDllJna.instanceDll.preComVoid_trans(APP, "1114", "1000", retdata);

			// 实名支付 -- 走不通
//			ret = MisDllJna.instanceDll.payRname_trans(APP, "1000", "姓名", "身份证/手机号",retdata);

			// 结算
//			ret = MisDllJna.instanceDll.settle_trans(APP, "1", retdata);

			// 结算单重打印
//			ret = MisDllJna.instanceDll.print_reply_trans(APP, "1114", retdata);

			// 交易结果查询 -- 不支持该交易
//			ret = MisDllJna.instanceDll.qry_trans(APP, "000001", retdata);

			// 退货
//			ret = MisDllJna.instanceDll.refund_trans(APP, "10000",  "111111111111", "1114",  retdata);

			// 微信支付宝扫码付
//			ret = MisDllJna.instanceDll.zw_trans(APP, "1",  "135722409919783802",  retdata);

			// 微信支付宝交易查询 -- 找不到交易
//			ret = MisDllJna.instanceDll.zw_balance_trans(APP, "1420171913211114",  retdata);

			// 微信支付宝退款
//			ret = MisDllJna.instanceDll.zw_refund_trans(APP, "1","1412171928211115",  retdata);
		}
		// 三方订单版本余额查询
//		ret = MisDllJnaThird.instanceDll.balance_trans(APP, retdata);
		// 三方订单消费
		ret = MisDllJnaThird.instanceDll.sale_trans_dsf(APP, "1", "\"orderid\":\"1234457890\",\"remark\": \"测试信息\"",  retdata);
		// 三方订单号测试
//		ret = MisDllJnaThird.instanceDll.qry_trans_dsf(APP, "1","135691988680472341", "\"orderid\":\"1234457890\",\"remark\": \"测试信息\"",  retdata);
		// 微信/支付宝三方订单支付
//		ret = MisDllJnaThird.instanceDll.zw_trans_dsf(APP, "1","135636939816662840", "\"orderid\":\"1234457890\",\"remark\": \"测试信息\"",  retdata);
		// 微信支付宝撤销
//		ret = MisDllJnaThird.instanceDll.zw_refund_trans(APP, "1","1414172084211117",   retdata);
		Map<String, String> data = parseRetMsg(retdata.getValue());
		Set<String> keys = data.keySet();
		for(String key : keys) {
			System.out.println("[" + key + "]" + "[" + data.get(key) + "]");
		}
	}
}
